/************************************************************
  *  * EaseMob CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of EaseMob Technologies.
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from EaseMob Technologies.
  */

#import "EMChatViewCell.h"
#import "EMChatVideoBubbleView.h"
#import "UIResponder+Router.h"

#define kPokeAndKissHeight 20.0
NSString *const kResendButtonTapEventName = @"kResendButtonTapEventName";
NSString *const kShouldResendCell = @"kShouldResendCell";

@implementation EMChatViewCell

- (id)initWithMessageModel:(MessageModel *)model reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithMessageModel:model reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.headImageView.clipsToBounds = YES;
        self.headImageView.layer.cornerRadius = 3.0;
    }
    return self;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGRect bubbleFrame = _bubbleView.frame;
    bubbleFrame.origin.y = self.headImageView.frame.origin.y;
    
    if (self.messageModel.isSender) {
        bubbleFrame.origin.y = self.headImageView.frame.origin.y;
        // 菊花状态 （因不确定菊花具体位置，要在子类中实现位置的修改）
        //未读
        _hasReadImage.hidden = YES;
        switch (self.messageModel.status) {
            case eMessageDeliveryState_Delivering:
            {
              
                [_messageType setHidden:YES];
               
                [_activityView setHidden:NO];
                [_activtiy startAnimating];
                [_retryButton setHidden:YES];
            }
                break;
            case eMessageDeliveryState_Delivered:
            {
               [_activtiy stopAnimating];
                [_retryButton setHidden:YES];
                [_messageType setHidden:NO];
                [_activityView setHidden:NO];
                if (self.messageModel.message.isReadAcked)
                {
                    //已读
                   // _hasRead.text = @"";
                    
                    _hasReadImage.hidden = YES;
                    //_hasRead.backgroundColor = [UIColor clearColor];
                }else{
                    _hasReadImage.hidden = NO;
                }
                
                if ([[self.messageModel.message.ext objectForKey:LITTLE_ACTION_TYPE_CHUO]isEqualToString:LITTLE_ACTION_TYPE_CHUO_VALUE]){
                    // _activityView.hidden = NO;
                    
                        _messageType.frame = CGRectMake(5, 5, kPokeAndKissHeight, kPokeAndKissHeight);
                        _messageType.image = [UIImage imageNamed:@"chat_戳一下"];
                   
                }else if ([[self.messageModel.message.ext objectForKey:LITTLE_ACTION_TYPE_MOMODA]isEqualToString:LITTLE_ACTION_TYPE_MOMODA_VALUE]){
                   // _activityView.hidden = NO;
                      _messageType.frame = CGRectMake(5, 5, kPokeAndKissHeight, kPokeAndKissHeight);
                        _messageType.image = [UIImage imageNamed:@"chat_么么哒"];
                    
                }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@true]){
                  //  _activityView.hidden = NO;
                    _messageType.frame = CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
                    _messageType.image = [UIImage imageNamed:@"news_chat_missed-calls_icon"];
                    
                }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@false]){
                   // _activityView.hidden = NO;
                    _messageType.frame = CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
                    _messageType.image = [UIImage imageNamed:@"news_chat_-call-time_icon"];
                    
                }else{
                  //  _activityView.hidden = NO;
                    _messageType.frame = CGRectMake(0, 0, 0, 0);
                    _messageType.image = [UIImage imageNamed:@"chat_clear_color"];
                }
                
            }
                break;
            case eMessageDeliveryState_Pending:
            case eMessageDeliveryState_Failure:
            {
                _messageType.frame = CGRectMake(0, 0, 0, 0);
                [_activtiy stopAnimating];
                [_messageType setHidden:YES];
                [_retryButton setHidden:NO];
                [_activityView setHidden:NO];

            }
                break;
            default:
                break;
        }
   
        

        
        //_hasReadImage.opaque = YES;

        

       
        bubbleFrame.origin.x = self.headImageView.frame.origin.x - bubbleFrame.size.width - HEAD_PADDING;
        _bubbleView.frame = bubbleFrame;
        
        CGRect frame = self.activityView.frame;
        frame.origin.x = bubbleFrame.origin.x - frame.size.width - ACTIVTIYVIEW_BUBBLE_PADDING;
        frame.origin.y = _bubbleView.center.y - frame.size.height / 2;
        self.activityView.frame = frame;

    }
    else{
        bubbleFrame.origin.x = HEAD_PADDING * 2 + HEAD_SIZE;
        if (_BurnAfterRreadingLabel.hidden == NO){
            bubbleFrame.size.width = 150;
            _bubbleView.frame = bubbleFrame;
            _BurnAfterRreadingLabel.frame = CGRectMake(0, 0, 150, _bubbleView.frame.size.height);
            [_BurnAfterRreadingLabel setHidden:NO];
        }else{
            bubbleFrame.size.width = bubbleFrame.size.width;
            _bubbleView.frame = bubbleFrame;
            [_BurnAfterRreadingLabel setHidden:YES];
            _BurnAfterRreadingLabel.userInteractionEnabled = NO;
        }
       

        if ([[self.messageModel.message.ext objectForKey:LITTLE_ACTION_TYPE_CHUO]isEqualToString:LITTLE_ACTION_TYPE_CHUO_VALUE]){
            [_messageType setHidden:NO];
            
                _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2+SEND_STATUS_SIZE/2-kPokeAndKissHeight/2, kPokeAndKissHeight,kPokeAndKissHeight);
                _messageType.image = [UIImage imageNamed:@"chat_戳一下"];
          
        }else if ([[self.messageModel.message.ext objectForKey:LITTLE_ACTION_TYPE_MOMODA]isEqualToString:LITTLE_ACTION_TYPE_MOMODA_VALUE]){
             [_messageType setHidden:NO];
             _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2+SEND_STATUS_SIZE/2-kPokeAndKissHeight/2, kPokeAndKissHeight,kPokeAndKissHeight);
                _messageType.image = [UIImage imageNamed:@"chat_么么哒"];
            
        }else if ([[self.messageModel.message.ext objectForKey:@"type"]isEqualToString:EMMESSAGE_EXT_TYPE_BURN_AFTER_READ_MSG]){
             [_messageType setHidden:NO];
            _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
            _messageType.image = [UIImage imageNamed:@"chat_simi"];
            
        }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@true]){
             [_messageType setHidden:NO];
            _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
            _messageType.image = [UIImage imageNamed:@"news_chat_missed-calls_icon"];
        }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@false]){
             [_messageType setHidden:NO];
            _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
            _messageType.image = [UIImage imageNamed:@"news_chat_-call-time_icon"];
            
        }else{
             [_messageType setHidden:YES];
            _messageType.image = [UIImage imageNamed:@"chat_clear_color"];
            _messageType.frame = CGRectMake(0, 0, 0, 0);
        }
        
    }
}

-(void)BurnafterreadingHidden:(UITapGestureRecognizer*)sender
{
//    [sender.view setHidden:YES];
}

- (void)setMessageModel:(MessageModel *)model
{
    [super setMessageModel:model];
    
    if (model.messageType != eMessageTypeChat) {
        _nameLabel.text = model.nickName;
        _nameLabel.hidden = model.isSender;
    }
    
    _bubbleView.model = self.messageModel;
    [_bubbleView sizeToFit];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - action

// 重发按钮事件
-(void)retryButtonPressed:(UIButton *)sender
{
    [self routerEventWithName:kResendButtonTapEventName
                     userInfo:@{kShouldResendCell:self}];
}

#pragma mark - private

- (void)setupSubviewsForMessageModel:(MessageModel *)messageModel
{
    [super setupSubviewsForMessageModel:messageModel];

    //类型
    _messageType = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE)];
    _messageType.hidden = YES;
    
    if (messageModel.isSender) {
  
        
        // 发送进度显示view
        _activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE)];
        [_activityView setHidden:YES];
        [self.contentView addSubview:_activityView];
        [_activityView addSubview:_messageType];
        
        // 重发按钮
        _retryButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _retryButton.frame = CGRectMake( 0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
        [_retryButton addTarget:self action:@selector(retryButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        //        [_retryButton setImage:[UIImage imageNamed:@"messageSendFail.png"] forState:UIControlStateNormal];
        [_retryButton setBackgroundImage:[UIImage imageNamed:@"messageSendFail"] forState:UIControlStateNormal];
        //[_retryButton setBackgroundColor:[UIColor redColor]];
        [_activityView addSubview:_retryButton];

        // 菊花
        _activtiy = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _activtiy.backgroundColor = [UIColor clearColor];
        [_activityView addSubview:_activtiy];
 
        //已读
       
        _hasReadImage = [[UIImageView alloc] initWithFrame:CGRectMake(SEND_STATUS_SIZE-9-5, SEND_STATUS_SIZE/2.0-9/2.0, 9, 9)];
        _hasReadImage.image = [UIImage imageNamed:@"news_chat_unread_icon"];
        _hasReadImage.hidden = YES;
        [_hasReadImage setContentMode:UIViewContentModeScaleAspectFill];
        [_activityView addSubview:_hasReadImage];
        
       
  
    }else{
         [self.contentView addSubview:_messageType];
    }
    
 
    _bubbleView = [self bubbleViewForMessageModel:messageModel];
    [self.contentView addSubview:_bubbleView];
    
    _BurnAfterRreadingLabel = [[UILabel alloc]initWithFrame:_bubbleView.frame];
    _BurnAfterRreadingLabel.text = @"  点击查看私密消息";
    _BurnAfterRreadingLabel.numberOfLines = 0;
    _BurnAfterRreadingLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _BurnAfterRreadingLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE];
    _BurnAfterRreadingLabel.backgroundColor = RGBA(209, 224, 225, 1);
    _BurnAfterRreadingLabel.userInteractionEnabled = NO;
    _BurnAfterRreadingLabel.multipleTouchEnabled = NO;
    [_bubbleView addSubview:_BurnAfterRreadingLabel];
    [_BurnAfterRreadingLabel setHidden:YES];

}

- (EMChatBaseBubbleView *)bubbleViewForMessageModel:(MessageModel *)messageModel
{
    switch (messageModel.type) {
        case eMessageBodyType_Text:
        {
            return [[EMChatTextBubbleView alloc] init];
        }
            break;
        case eMessageBodyType_Image:
        {
            return [[EMChatImageBubbleView alloc] init];
        }
            break;
        case eMessageBodyType_Voice:
        {
            return [[EMChatAudioBubbleView alloc] init];
        }
            break;
        case eMessageBodyType_Location:
        {
            return [[EMChatLocationBubbleView alloc] init];
        }
            break;
        case eMessageBodyType_Video:
        {
            return [[EMChatVideoBubbleView alloc] init];
        }
            break;
        default:
            break;
    }
    
    return nil;
}

+ (CGFloat)bubbleViewHeightForMessageModel:(MessageModel *)messageModel
{
    switch (messageModel.type) {
        case eMessageBodyType_Text:
        {
            return [EMChatTextBubbleView heightForBubbleWithObject:messageModel];
        }
            break;
        case eMessageBodyType_Image:
        {
            return [EMChatImageBubbleView heightForBubbleWithObject:messageModel];
        }
            break;
        case eMessageBodyType_Voice:
        {
            return [EMChatAudioBubbleView heightForBubbleWithObject:messageModel];
        }
            break;
        case eMessageBodyType_Location:
        {
            return [EMChatLocationBubbleView heightForBubbleWithObject:messageModel];
        }
            break;
        case eMessageBodyType_Video:
        {
            return [EMChatVideoBubbleView heightForBubbleWithObject:messageModel];
        }
            break;
        default:
            break;
    }
    
    return HEAD_SIZE;
}

#pragma mark - public

+ (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath withObject:(MessageModel *)model
{
    NSInteger bubbleHeight = [self bubbleViewHeightForMessageModel:model];
    NSInteger headHeight = HEAD_PADDING * 2 + HEAD_SIZE;
    if ((model.messageType != eMessageTypeChat) && !model.isSender) {
        headHeight += NAME_LABEL_HEIGHT;
    }
    return MAX(headHeight, bubbleHeight + NAME_LABEL_HEIGHT + NAME_LABEL_PADDING) + CELLPADDING;

}


@end
